﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RetingManager
{
    using Core;
    using System.Diagnostics;
    using System.IO;
    using System.Threading;

    public partial class RMForm : Form
    {
        string file = "";

        public RMForm(string[] args)
        {
            InitializeComponent();
            try
            {
                file = Application.StartupPath;
                if (file[file.Length - 1] != '\\')
                {
                    file += '\\';
                }
                file += "options.ini";
                if (File.Exists(file))
                {
                    ApplicationOptions.GetOptions(file);
                }
                else
                {
                    FileStream x = File.Create(file);
                    StreamWriter writer = new StreamWriter(x);
                    writer.Write("<Options />");
                    writer.Close();
                    x.Close();
                }
                if (args != null)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (File.Exists(args[i]))
                        {
                            SchoolClass value = RetingManagement.ReadData(args[i]);
                            DataForm form = new DataForm(value);
                            form.MdiParent = this;
                            form.Show();
                        }
                    }
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message, "Ошибка запуска", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void новыйРейтингToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateForm form = new CreateForm();
            form.MdiParent = this;
            form.Show();
            form.WindowState = FormWindowState.Normal;
        }
        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
           this.Close();
        }
        private void открытьHTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
        

        #region Дополнительные потоки для медленных компов

        private void hTMLОтчётToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HTMLReport report = new HTMLReport();
            report.MdiParent = this;
            report.Show();
        }
        private void таблицаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataView data = new DataView();
            data.MdiParent = this;
            data.Show();
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            HTMLReport report = new HTMLReport("Отчёт по рейтингу", openFileDialog1.FileName);
            report.MdiParent = this;
            report.Show();
        }
        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                DataForm loader = new DataForm(RetingManagement.ReadData(openFileDialog2.FileName));
                loader.MdiParent = this;
                loader.Show();
            }
            catch
            {
                MessageBox.Show("Ошибка загрузки данных");
            }
        }
        
        #endregion

        private void загрузитьДанныеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }
        
        private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateForm form = new CreateForm();
            form.MdiParent = this;
            form.Show();
        }
        private void загрузитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }
        private void выходToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void справкаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelpWnd wnd = new HelpWnd();
            wnd.ShowDialog();
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Вы действительно хотите выйти?\nВсё сделали?\nДанные сохранены?", 
                "Менеджер Рейтинга",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                Thread my = new Thread(new ThreadStart(SaveOptions));
                my.Start();
                this.Hide();
            }
            else
            {
                e.Cancel = true;
            }
        }
        private void SaveOptions()
        {
            ApplicationOptions.SetOptions(file);
        }
        private void плагиныToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PlugForm form = new PlugForm();
            form.ShowDialog();
        }

    }

}
